﻿Public Class Licenses

    Friend FDatabase As Database = Nothing
    Friend FLicenseData As Data.LicensesDataTable = Nothing

    Friend Sub New(ByVal database As Database)
        FDatabase = database
        FLicenseData = database.Data.Licenses
    End Sub

    Public Function GetAllLicenses() As List(Of License)

        ' Get all of the available licenses
        Dim Result As New List(Of License)
        For Each Row As Data.LicensesRow In FLicenseData
            Result.Add(CreateLicense(Row))
        Next

        ' Return the list of licenses
        Return Result

    End Function

    Public Function FindLicense(ByVal licenseName As String) As License

        ' Find the license
        licenseName = licenseName.ToUpper.Trim
        For Each Row As Data.LicensesRow In FLicenseData
            If Row.Name.ToUpper.Trim = licenseName Then
                Return CreateLicense(Row)
            End If
        Next

        ' The license could not be found
        Return Nothing

    End Function

    Public Function DoesLicenseExist(ByVal licenseID As Guid) As Boolean
        Return FLicenseData.FindByLicenseID(licenseID) IsNot Nothing
    End Function

    Public Function GetLicense(ByVal licenseID As Guid) As License
        Return CreateLicense(FLicenseData.FindByLicenseID(licenseID))
    End Function

    Public Sub AddLicense(ByVal license As License)

        ' Create the row for the license in the XML database
        Dim LicenseID As Guid = Guid.NewGuid
        license.LicenseID = LicenseID
        FLicenseData.AddLicensesRow(LicenseID, license.Name, license.Details, license.Number)

        ' Save the database
        FDatabase.Save()

    End Sub

    Public Sub DeleteLicense(ByVal license As License)
        DeleteLicense(license.LicenseID)
    End Sub

    Public Sub DeleteLicense(ByVal licenseID As Guid)

        ' Remove the license row
        FLicenseData.RemoveLicensesRow(FLicenseData.FindByLicenseID(licenseID))

        ' Save changes
        FDatabase.Save()

    End Sub

    Public Sub EditLicense(ByVal licenseID As Guid, ByVal license As License)

        ' Internal check
        If license.LicenseID <> licenseID Then Throw New ApplicationException("A license is being edited but an invalid license ID was provided.")

        ' Change the main license row
        Dim LicenseRow As Data.LicensesRow = FLicenseData.FindByLicenseID(licenseID)
        LicenseRow.LicenseID = license.LicenseID
        LicenseRow.Name = license.Name
        LicenseRow.Details = license.Details
        LicenseRow.Number = license.Number

        ' Save changes
        FDatabase.Save()

    End Sub

    Private Function CreateLicense(ByVal licenseRow As Data.LicensesRow) As License

        Dim Result As New License(licenseRow.Name)
        Result.LicenseID = licenseRow.LicenseID
        Result.Details = licenseRow.Details
        Result.Number = licenseRow.Number
        Return Result

    End Function

End Class
